package com.onpositive.wikitemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Locale;

public class DefaultTemplateResolver {
	
	static protected HashSet<String> depricatedTemplates = new HashSet<String>( Arrays.asList( new String[]{
			"#tag",
			"stack",
			"cite web",
			"cite book",
			"cite news",
			"cite journal",
			"cite encyclopedia",
			"citation",
			"citation needed",
			"reflist"
		})) ;
	
public static String resolveTemplates(String str,ITemplateSelector sel)		
	{
		try{
		int l = str.length() ;
		StringBuilder builder = new StringBuilder() ;
		int prev = 0 ;								
		for( int i  = str.indexOf("{{") ; i >= 0 ; i = str.indexOf("{{", prev ) )
		{
			if( prev < i-1 ){
				builder.append( str.substring(prev,i).trim() ) ;
				if( i > 0 && Character.isWhitespace( str.charAt(i-1)) )
					builder.append( " " ) ;
			}
			i += 2 ;
			prev = str.indexOf("}}",i) ;
			prev = prev < 0 ? str.length() -2 : prev ;
			
			String wholeTemplateString = str.substring(i-2, prev+2);
			
			int ind = str.indexOf("|",i) ;
			ind = ind < 0 ? l : ind ;
			ind = Math.min( ind, prev ) ;
			
			String templateName = str.substring(i,ind) ;
			
			if((sel!=null&&!sel.accept(templateName))||  isDepricated(templateName) ){
				prev += 2 ;
				continue ;
			}
			
			int ind_ = Math.min( ind+1, prev) ;
			String newString = DefaultTemplateResolver.resolveValue( templateName, str.substring(ind_,prev), wholeTemplateString ) ;			
			if( newString.length() != 0 ){
				builder.append( newString ) ;
				builder.append(" ") ;
			}
			prev += 2 ;
		}
		builder.append( str.substring( prev, str.length() ) ) ;
		String result = builder.toString().trim() ;								
		return result;
		}
		catch(Exception e){
			System.err.println(str);
			e.printStackTrace();
			return str;
		}
	}



	public static boolean isDepricated( String templateName )
	{
		return depricatedTemplates.contains(templateName.toLowerCase().trim() ) ;
	}

	private static String resolveValue(String templateName, String str, String wholeTemplateString )
	{
		if( str.trim().length() == 0 )
		{
			String result = DefaultNoParamsTemplateResolver.getinstance().resolve(templateName) ;
			if( result != null )
				return result ;
		}			
			
		BasicTemplateResolver basicResolver = DefaultTemplateResolverProvider.getInstance().getResolver(templateName) ;
		if( basicResolver != null ){
			try {
				final String result = basicResolver.resolveValue(str);
				return result != null ? result : "";
			} catch (Exception e) {
				System.err.println("Template resolve failure: " + str);
				e.printStackTrace();
			}
		}
		return wholeTemplateString ;
	}

	public static ArrayList<String> split(String str) {
		ArrayList<String> arr = new ArrayList<String>() ;
		int prev = 0 ; 
		for( int i = str.indexOf("|") ; i >= 0 ; i = str.indexOf("|",prev) )
		{
			String s = str.substring(prev, i) ;
			if( s.length() != 0 )
				arr.add(s.trim()) ;
			
			prev = i+1 ;						
		}
		if( prev < str.length()-1 )
			arr.add(str.substring(prev).trim()) ;
		return arr;
	}

}
